package xj.model.uml;

import static xj.model.uml.UMLModelConstants.ATTR_NAME;
import static xj.model.uml.UMLModelConstants.ATTR_VISIBILITY;
import static xj.model.uml.UMLModelConstants.ATTR_PACKAGE_NAME;

import xj.meta.ModelElement;

public abstract class NamedElement 
  extends BasicModelElement {

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
    setAttribute(ATTR_NAME, name);
  }

  public String getPackageName() {
    return packageName;
  }

  public void setPackageName(String packageName) {
    this.packageName = packageName;
    setAttribute(ATTR_PACKAGE_NAME, packageName);
  }

  public static final String PACKAGE_SEPARATOR = ".";

  public String getQualifiedName() {
    if (name != null) {
      ModelElement owner = getOwner();
      if (owner instanceof UMLPackage) {
	UMLPackage pkg = (UMLPackage) owner;
	String pname = pkg.getQualifiedName();
	if (pname != null) {
	  return pname + PACKAGE_SEPARATOR + name;
	}
      }
    }
    return name;
  }

  public Visibility getVisibility() {
    return visibility;
  }

  public void setVisibility(Visibility visibility) {
    this.visibility = visibility;
    setAttribute(ATTR_VISIBILITY, visibility);
  }

  @Override
  public Object clone() throws CloneNotSupportedException {
    NamedElement ne = (NamedElement) super.clone();
    ne.setName(name);
    ne.setPackageName(packageName);
    ne.setVisibility(visibility);
    return ne;
  }

  protected NamedElement() {
  }

  protected NamedElement(String name) {
    setName(name);
  }

  protected NamedElement(String name, Visibility visibility) {
    setName(name);
    setVisibility(visibility);
  }

  protected String name;

  protected String packageName;

  private Visibility visibility;

}
